﻿using DMS.Common.Model.Result;
using SqlSugar.Template.IService;
using SqlSugar.Template.IService.Param;
using SqlSugar.Template.IService.Result;
using SqlSugar.Template.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SqlSugar.Template.Service
{
    public class StockLstService : BaseService<StockLst>, IStockLstService
    {
        public async Task<ResponseResult> Add(AddStockLstParam param)
        {
            ResponseResult result = new();
            StockLst stock = new()
            {
                BookId = param.BookId,
                StockCode = param.StockCode,
                StockPrice = param.StockPrice,
                Picture = param.Picture,
                ClosingCode = param.ClosingCode,
                ClosePrice = param.ClosePrice
            };
            //插入返回自增列
            var flag = await base.InsertReturnIdentityAsync(stock);
            //null 列不插入
            //flag = await base.AsInsertable(stock).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
            result.data = flag;
            return result;
        }

        public Task<ResponseResult> AddTran(AddStockLstParam param)
        {
            throw new NotImplementedException();
        }

        public async Task<ResponseResult> DeleteAsync(long BookId)
        {
            ResponseResult result = new();
            var t1 = await Context.Deleteable<StockLst>()
                .Where(a => a.BookId == BookId)
                .ExecuteCommandAsync();
            result.data = t1;
            return result;
        }

        public async Task<ResponseResult> DeleteFileAsync(string  imageUrl)
        {
            ResponseResult result = new();
            var t1 = await Context.Deleteable<StockLst>()
                .Where(a => a.Picture == imageUrl)
                .ExecuteCommandAsync();
            result.data = t1;
            return result;
        }

        public async Task<ResponseResult<List<StockLstResult>>> GetStockAsync(long bookId)
        {
            ResponseResult<List<StockLstResult>> result = new() { data = new List<StockLstResult>() };
            var list = await Context.Queryable<StockLst>()
                 .Where(q => q.BookId.Equals(bookId))
                .Select<StockLstResult>()
                .ToListAsync();
            if (list == null)
            {
                result.errno = 1;
                result.errmsg = "未找到相关数据";
                return result;
            }
            result.data = list;
            return result;
        }

        public async Task<ResponseResult<List<StockLstResult>>> GetLstStockByStockCodeAndClosingCodeAsync(string StockCode, string ClosingCode)
        {
            ResponseResult<List<StockLstResult>> result = new()
            {
                data = new List<StockLstResult>()
            };
            var list = await Context.Queryable<StockLst>()
                .WhereIF(!string.IsNullOrWhiteSpace(StockCode), q => q.StockCode.Equals(StockCode)).
                WhereIF(!string.IsNullOrWhiteSpace(ClosingCode), q => q.ClosingCode.Equals(ClosingCode))
                .Select<StockLstResult>()
                .ToListAsync();
            if (list == null || list.Count <= 0)
            {
                result.errno = 2;
                result.errmsg = "未找到相关数据";
                return result;
            }
            result.data = list;
            return result;

        }

        public Task<ResponseResult<PageModel<StockLstResult>>> SearchBookAsync(SearchBookParam param)
        {
            throw new NotImplementedException();
        }

        public async Task<ResponseResult> UpdateAsync(EditStockLstParam param)
        {
            ResponseResult result = new();
            var t1 = await Context.Updateable(
             new StockLst()
             {
                 BookId = param.BookId,
                 StockCode = param.StockCode,
                 StockPrice = param.StockPrice,
                 ClosingCode = param.ClosingCode,
                 ClosePrice = param.ClosePrice,                
             })
             .Where(a => a.BookId == param.BookId)
             .ExecuteCommandAsync();
            result.data = t1;
            return result;
        }

        public async Task<ResponseResult> UpdateByIdAsync(StockLst param)
        {
            ResponseResult result = new();
            var t1 = await Context.Updateable(
             new StockLst()
             {         
                 Picture = param.Picture,
             })
             .Where(a => a.BookId == param.BookId)
             .ExecuteCommandAsync();
            result.data = t1;
            return result;
        }
    }
}
